package fr.dauphine.bookstore.backoffice.book;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import fr.dauphine.bookstore.hibernate.HibernateUtil;
import fr.dauphine.bookstore.modele.Book;

/**
 * Suppression d'un livre.
 */
public final class RemoveBookServlet extends HttpServlet {

	/**
	 * Serial version UID.
	 */
	private static final long serialVersionUID = 6691764298927858547L;

	/**
	 * Traite la methode GET.
	 * 
	 * @param request
	 *            Requete
	 * @param response
	 *            Reponse
	 * @throws ServletException
	 *             Erreur de servlet
	 * @throws IOException
	 *             Erreur d'entree/sortie
	 */
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();

		Long id = Long.valueOf(request.getParameter("id"));
		Book b = (Book) hSession.get(Book.class, id);

		hSession.delete(b);
		response.sendRedirect("books.jsp");
	}
}
